//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbNganh
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNganh_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbNganh_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbNganh OTbNganh)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenNganh", OTbNganh.TenNganh);
            pr[1] = new SqlParameter(@"TrangThai", OTbNganh.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbNganh_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbNganh OTbNganh)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaNganh", OTbNganh.MaNganh);
            pr[1] = new SqlParameter(@"TenNganh", OTbNganh.TenNganh);
            pr[2] = new SqlParameter(@"TrangThai", OTbNganh.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbNganh_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaNganh)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaNganh",MaNganh);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbNganh_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbNganh SelectByID(int MaNganh)
        {
            ETbNganh OTbNganh = new ETbNganh();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaNganh",MaNganh);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbNganh_SelectByID", pr);
            if (idr.Read())
              OTbNganh = GetOneTbNganh(idr);
            idr.Close();
            idr.Dispose();
            return OTbNganh;
        }
//-----------------------------------------------------------//
        private static ETbNganh GetOneTbNganh(IDataReader idr)
       {
            ETbNganh OTbNganh = new ETbNganh();
                if (idr["MaNganh"] != DBNull.Value)
                    OTbNganh.MaNganh = (int)idr["MaNganh"];
                if (idr["TenNganh"] != DBNull.Value)
                    OTbNganh.TenNganh = (string)idr["TenNganh"];
                if (idr["TrangThai"] != DBNull.Value)
                    OTbNganh.TrangThai = (int)idr["TrangThai"];
            return OTbNganh;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaNganh)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaNganh",MaNganh);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbNganh_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbNganh> ListAll()
        {
            List<ETbNganh> list = new List<ETbNganh>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbNganh_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbNganh(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbNganh> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbNganh> list = new List<ETbNganh>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbNganh_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbNganh(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}